use crate::i2s::regs::*;
use crate::i2s::types::I2s;

impl I2s {
    pub fn enable_tx_rx(&self) {
        unsafe {
            // global enable
            write_reg32(self.base(), FI2S_IER, 1);
            write_reg32(self.base(), FI2S_CER, 1);
            // channel 0 enable rx/tx
            write_reg32(self.base(), fi2s_rer0(0), 1);
            write_reg32(self.base(), fi2s_ter0(0), 1);
        }
    }

    pub fn reset_fifos(&self) {
        unsafe {
            write_reg32(self.base(), FI2S_RXFFR, 1);
            write_reg32(self.base(), FI2S_TXFFR, 1);
        }
    }

    pub fn set_clk_div(&self, _sample_rate: u32) {
        // TODO: program specific FRAC/EVE dividers if needed
    }

    pub fn set_dma_addrs(&self, rx_dev_off: usize, tx_dev_off: usize) {
        unsafe {
            write_reg32(self.base(), FI2S_RXDMA, rx_dev_off as u32);
            write_reg32(self.base(), FI2S_TXDMA, tx_dev_off as u32);
        }
    }

    pub fn stop(&self) {
        unsafe {
            write_reg32(self.base(), fi2s_rer0(0), 0);
            write_reg32(self.base(), fi2s_ter0(0), 0);
            write_reg32(self.base(), FI2S_CER, 0);
            write_reg32(self.base(), FI2S_IER, 0);
        }
    }
}


